-- local roleData = require "roleData"

require "timers"
local function checkTurn(data)
	local id = data.caster:GetPlayerOwnerID()
	if id ~= GameRules.curPlayer then
		showWarn("#TIP_NOT_YOUR_TURN",id)
		return false
	end
	return true
end
function HERO_TRADE( data )
	if not checkTurn(data) then
		
		return
	end
	local target = data.target
	local caster = data.caster
	local id1 = caster:GetPlayerOwnerID()
	local id2 = target:GetPlayerOwnerID()

	if id1 == id2 or not target:IsRealHero() then
		showWarn("#TIP_TRADE_TARGET_INVALID",id1)
		return
	end
	roleData.startTrade(id1,id2)
	
end

function HERO_DIYA( data )
	if not checkTurn(data) then
		
		return
	end
	local caster = data.caster
	local point = data.target_points[1]
	local cityid = block.getCityByPosition(point)
	if cityid then
		local city = roleData.getCityById(cityid)
		local idc = caster:GetPlayerOwnerID()
		local idt = city.owner
		if idt ~= idc then
			showWarn("#TIP_NOT_YOUR_CITY",idc)
			return
		end
		if city.lv > 1 then
			--盖了房子的只能先卖
			showWarn("#TIP_HASROOM_HERE",idc)
			return
		end
		if city.diya then
			showWarn("#TIP_HASBEEN_DIYA",idc)
			return
		end
		roleData.diyaCityById(idc,city.city)
		EmitGlobalSound("General.Buy")
	end
end

function HERO_SHUHUI( data )
	if not checkTurn(data) then
		
		return
	end
	local caster = data.caster
	local point = data.target_points[1]
	local cityid = block.getCityByPosition(point)
	if cityid then
		local city = roleData.getCityById(cityid)
		local idc = caster:GetPlayerOwnerID()
		local idt = city.owner
		if idt ~= idc then
			showWarn("#TIP_NOT_YOUR_CITY",idc)
			return
		end
		if city.diya then
			if not roleData.shCityById(idc,cityid) then
				showWarn("#TIP_NO_GOLD",idc)
			else
				EmitGlobalSound("General.Buy")
			end
		else
			showWarn("#TIP_NOTBEEN_DIYA",idc)
			return
		end
	end
end

function HERO_ROLL( data )
	if not checkTurn(data) then
		
		return
	end
	local caster = data.caster
	local id = caster:GetPlayerOwnerID()
	if caster.isMoving then
		showWarn("#TIP_IS_MOVING",id)
		return
	end
	local id = caster:GetPlayerOwnerID()
	if caster.actionDone then
		showWarn("#TIP_HAS_ROLLED",id)
		return
	end
	gameControl.doDice(caster)
	EmitGlobalSound("General.Acknowledge")
end

function LM_Ability( data )

end

function LM_Finish( data )
	dump(data)
end

function HERO_END( data )
	if not checkTurn(data) then
		
		return
	end
	local caster = data.caster
	local id = caster:GetPlayerOwnerID()
	if GameRules.custom_state ~= STATE_NONE then
		print("curState = ",GameRules.custom_state)
		showWarn("#TIP_CANOT_END_ROUND",id)
		return
	end
	
	if not caster.actionDone then
		showWarn("#TIP_NOT_FINISHI_ACTION",id)
		return
	end
	
	gameControl.endRound(caster:GetPlayerOwnerID())

end
--[[
	不要用removeAllAbility
]]
--护盾
function HERO_WUDI( data )
	if not checkTurn(data) then
		
		return
	end
	local caster = data.caster
	caster:AddNewModifier(caster, nil,"modifier_invulnerable", {})
	EmitGlobalSound("DOTA_Item.FaerieSpark.Activate")
	local ability = data.ability
	caster:RemoveAbility(ability:GetAbilityName())
end

function HERO_BUILD( data )
	if not checkTurn(data) then
		
		return
	end
	local caster = data.caster
	local point = data.target_points[1]
	local cityid = block.getCityByPosition(point)
	if cityid then
		local target = roleData.getCityById(cityid)
		local id = caster:GetPlayerOwnerID()
		local targetid = target.owner
		if id ~= GameRules.curPlayer then
			showWarn("#TIP_NOT_YOUR_TURN",id)
			return
		end
		if targetid ~= id then
			showWarn("#TIP_NOT_YOUR_CITY",id)
			return
		end
		--房子的升级要一间间升平衡等级
		local citiescanlvup = roleData.getCitiesCanLvup(id)
		for i,cityid in ipairs(citiescanlvup) do
			if cityid == target.city then
				roleData.lvupCityById(id,cityid)
				return
			end
		end
		showWarn("#TIP_CANOT_BUILD",id)
	end
end

function HERO_SELL( data )
	
	if not checkTurn(data) then
		
		return
	end
	local caster = data.caster
	local point = data.target_points[1]
	local cityid = block.getCityByPosition(point)
	if cityid then
		local target = roleData.getCityById(cityid)
		local id = caster:GetPlayerOwnerID()
		local targetid = target.owner
		if id ~= GameRules.curPlayer then
			showWarn("#TIP_NOT_YOUR_TURN",id)
			return
		end
		if targetid ~= id then
			showWarn("#TIP_NOT_YOUR_CITY",id)
			return
		end
		--房子的升级要一间间升平衡等级
		local citiescanlvup = roleData.getCitiesCanLvdown(id)
		for i,cityid in ipairs(citiescanlvup) do
			if cityid == target.city then
				roleData.delvCityById(id,cityid)
				return
			end
		end
		showWarn("#TIP_CANOT_SELL",id)
	end
end

function HERO_AOYUNHUI( data )
	local caster = data.caster
	local point = data.target_points[1]
	local cityid = block.getCityByPosition(point)

	if cityid then
		local city = roleData.getCityById(cityid)
		if not city.aoyunhui then
			city.aoyunhui = {}
		end
		if not city.suit then
			local id = caster:GetPlayerOwnerID()
			showWarn("#TIP_CANNOT_AYH_HERE",id)
		end
		local unit = CreateUnitByName("npc_aoyunhui", GameRules.blocks[cityid].unit:GetAbsOrigin(), true, nil, nil, caster:GetTeamNumber())
		local t = {owner = caster:GetPlayerOwnerID(),partID = partID,unit = unit}
		unit:SetAngles(0,270,0)
		table.insert(city.aoyunhui,t)
		caster:RemoveAbility(data.ability:GetAbilityName())	
	end
end

function HERO_TRAVEL( data )
	local caster = data.caster
	local point = data.target_points[1]
	local cityid = block.getCityByPosition(point)
	--TODO创建特效
	if cityid then
		local id = caster:GetPlayerOwnerID()
		local city = roleData.getCityById(cityid)
		if city.suit == 2 and city.owner == id then
			gameControl[caster:GetUnitName()](caster,cityid)
			caster:RemoveAbility(data.ability:GetAbilityName())
		else
			showWarn("#TIP_CANOT_TRAVEL_TOTHERE",id)
		end
	end
end

function HERO_TONADO( data )
	local caster = data.caster
	local point = data.target_points[1]
	local cityid = block.getCityByPosition(point)
	if cityid then
		local id = caster:GetPlayerOwnerID()
		local citiescanlvup = roleData.getCitiesCanLvdown(id)
		for i,_cityid in ipairs(citiescanlvup) do
			if _cityid == cityid then
				EmitSoundOn("n_creep_Wildkin.Tornado",city.unit)
				local idx = ParticleManager:CreateParticle("particles/units/heroes/hero_invoker/invoker_tornado_funnel_detail.vpcf",PATTACH_ABSORIGIN,city.unit)
				ParticleManager:SetParticleControl(idx,3,city.unit:GetAbsOrigin())
				GameRules:GetGameModeEntity():SetContextThink(DoUniqueString("destroyTornado"),
		            function()
		                ParticleManager:DestroyParticle(idx,true)
		            end,2)
				
				if roleData.delvCityById(id,cityid,true) then
					caster:RemoveAbility(data.ability:GetAbilityName())
				end
				return
			end
		end
	end
end

function HERO_OUT( data )
	local caster = data.caster
	if caster.isInJy == true then
		caster.isInJy = false
		ModifyGoldLtx(caster:GetPlayerOwnerID(),-100)
	end
end

function HERO_OUTWITHCARD( data )
	local caster = data.caster
	if caster.isInJy == true then
		caster.isInJy = false
		caster:RemoveAbility(data.ability:GetAbilityName())
	end
end

function OnAgentInit( data )
	
end

print("agent inited")